package org.darkforest.controller;

import java.util.Date;
import java.util.List;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.darkforest.auth.AuthPassport;
import org.darkforest.auth.CurrentMember;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.WinEggsMapper;
import org.darkforest.model.Member;
import org.darkforest.model.RestEntity;
import org.darkforest.model.WinEggs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "winEggs")
@Api(value = "winEggs", description = "天天领奖", produces = MediaType.APPLICATION_JSON_VALUE)
public class WinEggsController {

    Logger logger = LoggerFactory.getLogger(WinEggsController.class);

    @Autowired
    private WinEggsMapper winEggsMapper;


    /**
     *  用户今天砸蛋次数的取得
     *
     * @param member
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "getTodayInfo", method = RequestMethod.GET)
    @ApiOperation(value = "用户今天砸蛋次数的取得", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity getTodayInfo(@CurrentMember Member member) {
        try {

            // 今天 当前用户砸蛋次数的取得
            WinEggs record = new WinEggs();
            record.setMemberId(member.getMemberId());
            record.setClickDate(DateUtil.toDateString(new Date()));   // yyyy-MM-dd
            List<WinEggs> winEggsList = winEggsMapper.selectByCondition(record);

            // 如果取不到就登录（今天数据初期化）
            if (winEggsList == null || winEggsList.size() == 0) {
                record = new WinEggs();
                record.setMemberId(member.getMemberId());
                record.setClickDate(DateUtil.toDateString(new Date()));   // yyyy-MM-dd
                record.setClickNum(0);  // 砸蛋次数初期话
                winEggsMapper.insert(record);
            }  else {
                record = winEggsList.get(0);
            }

            return RestEntity.success(record);
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed("获取异常");
        }
    }

    /***
     * 今天数据初期化
     *
     * @param member
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "insertTodayInfo", method = RequestMethod.POST)
    @ApiOperation(value = "今天数据初期化", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity insertTodayInfo(@CurrentMember Member member) {
        try {

            WinEggs record = new WinEggs();
            record.setMemberId(member.getMemberId());
            record.setClickDate(DateUtil.toDateString(new Date()));   // yyyy-MM-dd
            record.setClickNum(0);  // 砸蛋次数初期化
            winEggsMapper.insert(record);

            return RestEntity.success(record);
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed("登录异常");
        }
    }

    /***
     * 用户今天砸蛋次数的更新
     *
     * @param member
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "updateTodayInfo", method = RequestMethod.POST)
    @ApiOperation(value = "用户今天砸蛋次数的更新", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity updateTodayInfo(@CurrentMember Member member, @RequestParam(required = true) Integer clickNum) {
        try {
            WinEggs record = new WinEggs();
            record.setMemberId(member.getMemberId());
            // setClickDate  sql中取得
            record.setClickNum(clickNum);

            winEggsMapper.updateTodayNumByMember(record);

            return RestEntity.success(record);
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed("更新异常");
        }
    }
}
